unit treasuremain;

interface
uses
  System.SysUtils;

const
  imp = 'IMPOSSIBLE';
type IntArray = array of Integer;
var
  f, g: TextFile;
  a: array [0..3] of array [0..3] of char;
  k, n: Integer;
  InitialKeysArray: array[1..400] of 1..200;
  Chests: array[1..200] of record
    KeyTypeToOpen: 1..200;
    NumberOfKeysInside: 1..400;
    KeysInsideArray: array[1..400] of 1..200;
  end;
  CurrentUnusedKeysCount: array[1..200] of 0..400;
  CurrentClosedChests: array[1..200] of Boolean;
  verdict: String;
  verdictAssigned, EmptySpacePresent: Boolean;

procedure ReadData;
procedure ProcessData;
procedure TryToOpenChest(chestIndex: Integer; var path: IntArray);
procedure OutputVerdict(casenum: Word);
implementation

procedure ReadData;
var
  i, j: Integer;
begin
  for i := 1 to 200 do begin
    CurrentUnusedKeysCount[i] := 0;
    CurrentClosedChests[i] := True;
  end;
  Readln(f, k, n);
  for i := 1 to k do begin
    Read(f, InitialKeysArray[i]);
    Inc(CurrentUnusedKeysCount[InitialKeysArray[i]]);
  end;
  Readln(f);
  for i := 1 to n do begin
    Read(f, Chests[i].KeyTypeToOpen);
    Read(f, Chests[i].NumberOfKeysInside);
    for j := 1 to Chests[i].NumberOfKeysInside do
      Read(f, Chests[i].KeysInsideArray[j]);
    Readln(f);
  end;
end;

procedure LookForClosedChest(var path: IntArray);
var
  i: Integer;
begin
  for i := 1 to n do
    if CurrentClosedChests[i] and (CurrentUnusedKeysCount[Chests[i].KeyTypeToOpen] > 0) then begin
      TryToOpenChest(i, path);
      if verdictAssigned then
        Exit;
    end;
end;

procedure AcquireKeysFromChest(chestIndex: Integer);
var
  j: Integer;
begin
  for j := 1 to Chests[chestIndex].NumberOfKeysInside do
    Inc(CurrentUnusedKeysCount[Chests[chestIndex].KeysInsideArray[j]]);
end;

procedure CancelAcquireKeysFromChest(chestIndex: Integer);
var
  j: Integer;
begin
  for j := 1 to Chests[chestIndex].NumberOfKeysInside do
    Dec(CurrentUnusedKeysCount[Chests[chestIndex].KeysInsideArray[j]]);
end;

function AllChestsOpened: Boolean;
var
  i: Integer;
begin
  for i := 1 to n do
    if CurrentClosedChests[i] then begin
      Result := false;
      Exit;
    end;
  Result := True;
end;

function AllKeysUsed: Boolean;
var
  i: Integer;
begin
  for i := 1 to 200 do
    if CurrentUnusedKeysCount[i] > 0 then begin
      Result := false;
      Exit;
    end;
  Result := True;
end;

function FormatPath(var path: array of Integer): String;
var
  len: Integer;
    i: Integer;
begin
  len := Length(path);
  Result := '';
  for i := 0 to len - 1 do
    Result := Result + IntToStr(path[i]) + ' ';
end;

procedure TryToOpenChest(chestIndex: Integer; var path: IntArray);
var
  len: Integer;

  procedure Cancel;
  begin
    Inc(CurrentUnusedKeysCount[Chests[chestIndex].KeyTypeToOpen]);
    CurrentClosedChests[chestIndex] := True;
    CancelAcquireKeysFromChest(chestIndex);
    len := Length(path);
    path[len] := -1;
    SetLength(path, len - 1);
  end;

begin
  Dec(CurrentUnusedKeysCount[Chests[chestIndex].KeyTypeToOpen]);
  CurrentClosedChests[chestIndex] := False;
  AcquireKeysFromChest(chestIndex);
  len := Length(path);
  SetLength(path, len + 1);
  path[len] := chestIndex;
  if AllChestsOpened then begin
    verdictAssigned := True;
    verdict := FormatPath(path);
    Exit;
  end;
  if AllKeysUsed then begin
    Cancel;
    Exit;
  end;
  LookForClosedChest(path);
  if not verdictAssigned then begin
    Cancel;
  end;
end;

procedure ProcessData;
var
  a: IntArray;
begin
  SetLength(a, 0);
  LookForClosedChest(a);
  if not verdictAssigned then
    verdict := imp;
end;

procedure OutputVerdict(casenum: Word);
begin
  writeln(g, 'Case #', casenum, ': ', verdict);
  writeln('Case #', casenum, ': ', verdict);
end;

end.
